package com.compus.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.json.JSONObject;
import com.compus.constant.OrderStatus;
import com.compus.entity.po.Order;
import com.compus.entity.vo.OrderVo;
import com.compus.entity.vo.Result;
import com.compus.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Tag(name = "订单")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Operation(summary = "用户预约车辆")
    @PostMapping("/userAddOrder")
    public Result<?> userAddOrder(@RequestBody Order order) {
        Long count = orderService.lambdaQuery()
                .eq(Order::getUserId, StpUtil.getLoginIdAsLong())
                .eq(Order::getStatus, OrderStatus.WAIT)
                .count();
        if (count > 0) {
            return Result.fail("您有未完成的订单,请取消后重试");
        }
        order.setUserId(StpUtil.getLoginIdAsLong());
        order.setCreateTime(LocalDateTime.now());
        order.setStatus(OrderStatus.WAIT);
        orderService.save(order);
        return Result.success();
    }

    @SaIgnore
    @Operation(summary = "查询订单")
    @GetMapping("/queryOrder")
    public Result<?> scanOrder(Long orderId) {
        Order order = orderService.getById(orderId);
        if (order == null) {
            return Result.fail("订单不存在");
        }
        if (Objects.equals(order.getStatus(), OrderStatus.FINISH)) {
            return Result.fail("已上车");
        }
        orderService.lambdaUpdate()
                .eq(Order::getId, orderId)
                .set(Order::getStatus, OrderStatus.FINISH)
                .update();
        return Result.success();
    }


    @Operation(summary = "生成订单二维码")
    @GetMapping("/createQrCode")
    public Result<String> createQrCode() {
        long userId = StpUtil.getLoginIdAsLong();
        Order order = orderService.lambdaQuery()
                .eq(Order::getUserId, userId)
                .eq(Order::getStatus, OrderStatus.WAIT)
                .orderByDesc(Order::getCreateTime)
                .last("limit 1")
                .one();
        if (order==null){
            return Result.fail("没有正在进行的订单");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("orderId", order.getId());
        jsonObject.set("name",StpUtil.getSession().get("name"));
        QrConfig qrConfig = new QrConfig();
        qrConfig.setHeight(300);
        qrConfig.setWidth(300);
    String string = QrCodeUtil.generateAsBase64(jsonObject.toJSONString(0), qrConfig, "png");
        return Result.success(string);
    }


    @Operation(summary = "查询订单列表")
    @GetMapping("/getOrderList")
    public Result<List<OrderVo>> listOrder() {
        List<OrderVo> orderVos = orderService.listOrder();
        return Result.success(orderVos);
    }

    @Operation(summary = "取消订单")
    @GetMapping("/cancelOrder")
     public Result<?> cancelOrder(Long orderId) {
        Order order = orderService.getById(orderId);
        if (order == null) {
            return Result.fail("订单不存在");
        }
        if (!Objects.equals(order.getStatus(), OrderStatus.WAIT)) {
            return Result.fail("订单状态不正确");
        }
        order.setStatus(OrderStatus.CANCEL);
        orderService.updateById(order);
        return Result.success();
    }
}
